package com.kdtech.analyse.video;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;

public class PeopleVideoAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		// http://v.jzrt.com/play.html?c=hue1l6d7c4sp1v43
		String[] regex = { 
				"http://tv.people.com.cn/.*/[0-9]+/[0-9]+/c[0-9]+-[0-9]+.html",
				"http://vblog.people.com.cn/index/playvideo/.*/contentid/[0-9]+",
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}

		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		video.setUrl(url);
		String title = null;
		Long date = 0l;

			Document doc = Jsoup.parse(html);
			title = doc.select("div.tit1  h2").text();
			if(StringUtils.isBlank(title)){
				title = doc.select("div.w1000  h2").text();
				if(StringUtils.isBlank(title)){
					title = doc.select("div.wb_left  h2").text();
				}
			}
			date = DateUtils.matchDate(doc.select("div.w1000").text());
			if(date==null)
			date = DateUtils.matchDate(doc.select("div.share").text());
			String content = doc.select("div.v-intro").text();
			String author = JSoupUtils.matchAuthor(doc, "来源：");
			video.setAuthor(author);
			video.setContent(content);
		video.setDate(date);
		if (StringUtils.isBlank(title)) {
		}
		video.setTitle(title);
		
		return video;
	}

	
	public NewsMeta Update(NewsMeta meta) {

		return null;
	}

	public static void main(String[] args) {
		PeopleVideoAnalyse a = new PeopleVideoAnalyse();
		String url = "http://tv.people.com.cn/n/2015/0626/c39805-27211633.html";
		if (a.isDetailPage(url)) {
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		} else {
			System.out.println("不匹配规则");
		}

	}
	

}
